// 引入接口文件
import {getProduct,regionArea,getReport,getChart,getContrast,down} from './codeChannelCanalsServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import { makeAddress ,download} from "utils/util"
// import { SIGUNUSED } from 'constants';

// 初始默认状态
const defultState = {
	area1:[],
	products:[],
	areaList:[],
	reports:[],
	showModal:false,
	loading:true,
	params:{
		tableRowType:'',
		productId:'',
		cityCode:[]
	},
	query1:{
		dataType:1,
		contrastType:'',
		statisticsContent:[],
		contrastData:[],
		regionId:null,
		productId:null,
	},
	time:{
		startTime:null,
		endTime:null
	},
	time1:{
		startTime:null,
		endTime:null
	},
	dateList:[],
	name:[],
	dataList:[],
	visible:false,
	contrast:[],
	params1:{
		productId:null,
		contrastData:null,
		contrastType:null
	},
	chart:null,
	roleType:4,
	statisticsContent:[],
	contrastType:'',
	regionId:null,
	productId:null,
	selectValue:[],
	title:null,
	productName:null,
	selected:'7',
	selected2:'7',
	graphRespDtos:[]
}

// 声明module
const tmpModule = {
	// 默认状态
	state: cloneDeep(defultState),
	// 入口函数(先执行)
	subscriptions: {
		setup({ dispatch, history }) {
			history.listen(({ pathname, query }) => {
				// 页面路由匹配时执行
				if ('' + tmpModule.pathname === '' + pathname) {
					let time={
						startTime:query.startTime || moment().subtract(6, 'days').format('YYYY-MM-DD'),
						endTime:query.endTime || moment().format('YYYY-MM-DD')
					}
					let statisticsContent=[]
					statisticsContent[0]=query.id
					let cityCode=[]
					if(query.provinceCode){
						cityCode[0]=query.provinceCode
						if(query.cityCode){
							cityCode[1]=query.cityCode
						}
					}
					let params={
						tableRowType:query.regionId,
						productId:query.productId,
						cityCode
					}
					dispatch({ type: 'updateStore', payload: {...cloneDeep(defultState),statisticsContent,contrastType:query.contrastType,productId:query.productId,time,time1:time,productName:query.title,regionId:query.regionId,selected:query.selected2,selected2:query.selected2,params
					} })
					dispatch({type:'regionArea',payload:{}})
					dispatch({type:'getReport',payload:{}})
					dispatch({type:'getChart',payload:{}})
				}
			})
		},
	},
	// 方法列表
	effects: {
		//获取区域
		*regionArea({payload},{put,call,select}){
			let {regionId,products,areaList}=yield select(state => state[tmpModule.namespace])
					const {success,data}=yield call(regionArea)
					if(success){
						if(regionId){
							data.map((item,index)=>{
								if(item.value==regionId){
									areaList = makeAddress(item.children,2)
								}
							})
						}
						yield put({type:'updateStore',payload:{area1:data,areaList}})
						yield put({type:'getProduct',payload:{}})
					}
				},
		*getProduct({payload},{put,call,select}){
			let {params,products} = yield select(state => state[tmpModule.namespace])
			if(params.cityCode.length>1){
				const { data, success } = yield call(getProduct,{cityCode:params.cityCode[1]});
				if(success){
					products=data
					yield put({ type: 'updateStore',payload:{products}})
				}
			}
			
		},	
		//获取类别渠道汇总
		*getReport({payload},{put,call,select}){
			let {productId,regionId,query1,statisticsContent,contrastType,time1,selected2}=yield select(state => state[tmpModule.namespace])
			let query={...query1,...payload,contrastType,statisticsContent,...time1,productId,regionId}
			delete query.contrastData
			let {success,data}=yield call(getReport,query)
			if(success){
				yield put({type:'updateStore',payload:{reports:data,loading:false,}})
			}
		},
		*getChart({payload},{put,call,select}){
			let {dateList,name,productId,regionId,dataList,query1,statisticsContent,contrastType,params1,time,title,graphRespDtos}=yield select(state => state[tmpModule.namespace])
			let query={...query1,statisticsContent,contrastType,...payload,...time,productId,regionId}
			let selectValue=[]
			let {data,success}=yield call(getChart,query)
			if(success){
				dateList=[],
				dataList=[],
				name=[]
				if(data){
					params1.contrastData=data.contrastData || null 
					params1.contrastType=data.contrastType || null
					title=data.title
				}else{
					params1.contrastData=null
					params1.contrastType=null
				}
				if(data && data.graphRespDtos){
					graphRespDtos=data.graphRespDtos
					for(let i in data.graphRespDtos){
						let obj={
							name:'',
							data:[],
							type:'line',
							symbol: 'circle',
							smooth: true,        
							symbolSize: 3,
						}
						obj.name=data.graphRespDtos[i].graphName
						name.push(data.graphRespDtos[i].graphName)
						if(!(data.graphRespDtos[i].graphType<0)){
							selectValue.push(data.graphRespDtos[i].graphType)
						}
						data.graphRespDtos[i].graphValues.map((item,index)=>{
							if(i==0){
								dateList.push(item.time)
							}
							obj.data.push(item.value)
						})
						dataList.push(obj)
					}
				}
			}  
			yield put({type:'updateStore',payload:{dataList,name,dateList,loading:false,params1,selectValue,title,graphRespDtos}})
		},
		*getContrast({payload},{put,call,select}){
			let {params1,productId,regionId}=yield select(state => state[tmpModule.namespace])
			let {data,success}=yield call(getContrast,{...params1,productId,regionId})
			if(success){
				yield put({type:'updateStore',payload:{contrast:data}})
			}
		},
		*down({payload},{put ,call,select}){
			let {productId,regionId,query1,statisticsContent,contrastType,time1}=yield select(state => state[tmpModule.namespace])
			let query={...query1,...payload,contrastType,statisticsContent,...time1,productId,regionId}
			let {data,success}=yield call(down,query)
			if(success){
				download(data)
			}
		}
	},
	// 存入状态机
	reducers: {
		updateStore(preState, action) {
			return Object.assign({}, preState, action.payload)
		},
	}
}
export default tmpModule
